﻿using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Hardware;
using Alarm;
using DeviceSolutions.SPOT.Hardware;
using Microsoft.SPOT.IO;
using System.IO;
using System.Text;
using System;

//******                      NOTICE   !!!                                                  \\\\
//******   PLease Notice That if uyou need to test this program, it's compulsory to use *****\\
//******   A SD-Card if not the program won't work. Please insert a sd-card no higher than 1GB and you need to create the file history.txt on this sd-card\\
//******   Finally you must insert the sd-card on the main page (where you can choose either to check lights or door or temperature or settings\\
//******   Insert it on this page exactly before continue\\
//******   :-)    \\





namespace HouseControl
{


    public class Program : Application
    {
        public static bool stateLed = true;  //stateled is used to define the status of the light in Room 1
        public static OutputPort led;        //Define an outputport for the light in room 1
        public static bool stateLeds = true;  //stateleds is used to define the status of the light in Room 2
        public static OutputPort leds;        //Define an outputport for the light in room 2
        public static InputPort inputPort;    //Define an inputport for the Door
        public static bool gate;               //will store the status of the door
        public static InterruptPort port = new InterruptPort(TahoeII.Pins.SW3,  //define SW1 as Button that can change the status of the light in room 1
                                                 false, //no glitch filter
                                                 Port.ResistorMode.PullUp, //work when the button is released
                                                 Port.InterruptMode.InterruptEdgeBoth); //define SW3 as Button that can change the status of the light in room 1
        public static InterruptPort ports = new InterruptPort(TahoeII.Pins.SW4,
                                                 false, //no glitch filter
                                                 Port.ResistorMode.PullUp, //work when the button is released
                                                 Port.InterruptMode.InterruptEdgeBoth);

        public static InterruptPort door_bell = new InterruptPort(TahoeII.Pins.SW5,   //for the bell 
                                                 false, //no glitch filter
                                                 Port.ResistorMode.PullUp, //work when the button is released
                                                 Port.InterruptMode.InterruptEdgeBoth);
        


        /// <summary>
        /// Thread to manage the server of the Tahoe II
        /// </summary>
        public static Thread SERVER = null;

        /// <summary>
        /// Thread to manage the Tahoe II System
        /// </summary>
        public static Thread TAHOE = null;

        /// <summary>
        /// Thread to manage the Alarm
        /// </summary>
        public static Thread ALARM = null;

        public static bool door_bell_state = false; //this variable will contain the statut of the bell of the door and trigger the alarm or not

        public static bool light_interaction = false; //value that help i=us to know whether an user changed through the server the c=status of the light

        public static bool theme_changed = false;  //to know wich theme to display false--<default ; true--> another theme

        public static float min_temp =-5;   //we define by default minimum temperature to (-1) 

        public static float max_temp =40;    //we define by default maximum temperature to (36) 

        public static bool alarm_min_reached = false;  //to know whether the limit is reached or not, when it's true the alarm is tigerred

        public static bool alarm_max_reached = false;  //to know whether the limit is reached or not, when it's true the alarm is tigerred

        public static Alarm_SPI alarm = new Alarm_SPI(); //variable that launch a new instance of the Alarm

        public static String[] read_database;  //array that store the content of the database

        public static bool max_temp_set = false;

        public static bool min_temp_set = false;

        public static void Main()
        {
            Cpu.Pin ledPin = Meridian.Pins.GPIO7;  //led in  GPIO7
            led = new OutputPort(ledPin, stateLed);//led is on at the begining of the program
            Cpu.Pin ledsPin = Meridian.Pins.GPIO9;  //led in  GPIO9
            leds = new OutputPort(ledsPin, stateLed);//led is on at the begining of the program
            Cpu.Pin inputPin = Meridian.Pins.GPIO8; //Read the switcher  on GPIO8
            Program.inputPort = new InputPort(inputPin, true, Port.ResistorMode.PullUp);// switcher "ON" state

           

            //initiate the server for the tahoe
            // SERVER = new Thread(new ThreadStart(server.startserver));
            //SERVER.Start();


            //  initiate the tahoe
            TAHOE = new Thread(new ThreadStart(initiate_tahoe));
            TAHOE.Start();

            //  initiate the Alarm
           //  ALARM = new Thread(new ThreadStart(Alarm.start_alarm_thread));
             //ALARM.Start();

        }

        public static void initiate_tahoe() //function that initiate the Tahoe Program
        {
           
                Bitmap bmp = Resources.GetBitmap(Resources.BitmapResources.images2); //draw welcome message
                GUI.DrawImage(bmp);
            
            Program myApplication = new Program();  //create a new instance o the Program

            Window mainWindow = CheckingsPage();  //launching the mainWindow

            Touch.Initialize(myApplication);  //enabling Touch control

            // Start the application
            myApplication.Run(mainWindow);
        }

        public static Window mainWindow;   //the main window

        public static Window firstroomSet;   //window of two rooms
        public static Window firstroomlightSet; //window of the first room
        public static Window lighton1Set;        //window of the first room light switching


        public static Window secondroomlightSet; //window of the second room
        public static Window lighton2Set;         //window of the first room light switching

        public static Window doorSet;  //window of the door control 

        public static Window heatSet;   //window for the heat control

        public static Window SettingsSet;   //window for the Settings 

        public static Window Settings_heat;  //window for the settings aboi=ut the Heat 
        public static Window Settings_help;  //window for the help

        public static Window CheckingsPage()
        {
            mainWindow = new Window();    //create a new window 
            //set his size to the screen size
            mainWindow.Height = SystemMetrics.ScreenHeight;
            mainWindow.Width = SystemMetrics.ScreenWidth;

            //we handle here the event when the SD-card is inserted or not 
            RemovableMedia.Insert += new InsertEventHandler(DataBase.RemovableMedia_Insert);

            //handle the event when someone push the button to make ring the door bell
            Program.door_bell.OnInterrupt += new NativeEventHandler(Door_Bell.bell_alarm_switcher); 
         
            if (theme_changed == false)  //theme control
            {

                Canvas canvas = new Canvas();

                //adding image checking button 
                Image imagelight = new Image(Resources.GetBitmap(Resources.BitmapResources.house1));
                SimpleButton imagelightButton = new SimpleButton(imagelight, 90, 120);
                Canvas.SetLeft(imagelightButton, 20);
                Canvas.SetTop(imagelightButton, 20);
                canvas.Children.Add(imagelightButton);
                imagelightButton.Click += new EventHandler(Lights.CheckingLightsPageIndex);

                //adding some text 
                Text light = new Text();
                light.Font = Resources.GetFont(Resources.FontResources.NinaB);
                light.ForeColor = Colors.Red;
                light.TextContent = "CHECK LIGHTS";
                Canvas.SetTop(light, 150);
                Canvas.SetLeft(light, 20);
                canvas.Children.Add(light);

                //adding door button
                Image imagedoor = new Image(Resources.GetBitmap(Resources.BitmapResources.house11));
                SimpleButton imagedoorButton = new SimpleButton(imagedoor, 90, 120);
                Canvas.SetRight(imagedoorButton, 20);
                Canvas.SetTop(imagedoorButton, 20);
                canvas.Children.Add(imagedoorButton);
                imagedoorButton.Click += new EventHandler(Door_Windows.CheckingDoorPageIndex);

                //adding text 
                Text door = new Text();
                door.Font = Resources.GetFont(Resources.FontResources.NinaB);
                door.ForeColor = Colors.Red;
                door.TextContent = "CHECK DOOR";
                Canvas.SetTop(door, 148);
                Canvas.SetRight(door, 20);
                canvas.Children.Add(door);

                //adding heat control button 
                Image imageheat = new Image(Resources.GetBitmap(Resources.BitmapResources.heatdisplay));
                SimpleButton imageheatButton = new SimpleButton(imageheat, 105, 34);
                Canvas.SetLeft(imageheatButton, 20);
                Canvas.SetBottom(imageheatButton, 20);
                canvas.Children.Add(imageheatButton);
                imageheatButton.Click += new EventHandler(Heat_Control.CheckingHeatPageIndex);

                //adding text
                Text heat = new Text();
                heat.Font = Resources.GetFont(Resources.FontResources.NinaB);
                heat.ForeColor = Colors.Red;
                heat.TextContent = "CHECK HEAT";
                Canvas.SetBottom(heat, 0);
                Canvas.SetLeft(heat, 32);
                canvas.Children.Add(heat);

                //adding settings button
                Image imagesettings = new Image(Resources.GetBitmap(Resources.BitmapResources.settings2));
                SimpleButton imagesettingsButton = new SimpleButton(imagesettings, 80, 60);
                Canvas.SetRight(imagesettingsButton, 20);
                Canvas.SetBottom(imagesettingsButton, 20);
                canvas.Children.Add(imagesettingsButton);
                imagesettingsButton.Click += new EventHandler(Settings.SettingsPageIndex);

                //text adding
                Text settings = new Text();
                settings.Font = Resources.GetFont(Resources.FontResources.NinaB);
                settings.ForeColor = Colors.Red;
                settings.TextContent = "SETTINGS";
                Canvas.SetBottom(settings, 0);
                Canvas.SetRight(settings, 32);
                canvas.Children.Add(settings);

                //display an image background
                ImageBrush background = new ImageBrush(Resources.GetBitmap(Resources.BitmapResources.HOME));
                background.Stretch = Stretch.Fill;
                Program.mainWindow.Background = background;


                // Add the text control to the window.
                Program.mainWindow.Child = canvas;



            }
            else if (theme_changed == true) //theme control
            {
                StackPanel stackPanel = new StackPanel();
                Image imagelight = new Image(Resources.GetBitmap(Resources.BitmapResources.house2));
                SimpleButton imagelightButton = new SimpleButton(imagelight, mainWindow.Width, 120);
                imagelightButton.Click += new EventHandler(Lights.CheckingLightsPageIndex);
                stackPanel.Children.Add(imagelightButton);

                Image imagedoor = new Image(Resources.GetBitmap(Resources.BitmapResources.door2));
                SimpleButton imagedoorButton = new SimpleButton(imagedoor, mainWindow.Width, 30);
                imagedoorButton.Click += new EventHandler(Door_Windows.CheckingDoorPageIndex);

                Image imageheat = new Image(Resources.GetBitmap(Resources.BitmapResources.heatdisplay1));
                SimpleButton imageheatButton = new SimpleButton(imageheat, mainWindow.Width, 34);
                imageheatButton.Click += new EventHandler(Heat_Control.CheckingHeatPageIndex);

                Image imagesettings = new Image(Resources.GetBitmap(Resources.BitmapResources.settings21));
                SimpleButton imagesettingsButton = new SimpleButton(imagesettings, mainWindow.Width, 75);
                imagesettingsButton.Click += new EventHandler(Settings.SettingsPageIndex);


                stackPanel.Children.Add(imagedoorButton);
                stackPanel.Children.Add(imageheatButton);
                stackPanel.Children.Add(imagesettingsButton);
                mainWindow.Child = stackPanel;
                stackPanel.SetMargin(0, 0, 0, 0);

            }

            mainWindow.Visibility = Visibility.Visible;

            return mainWindow;
        }
    }
}